package com.luckberry.murder.service.impl;

import com.luckberry.murder.entity.User;
import com.luckberry.murder.mapper.UserMapper;
import com.luckberry.murder.service.UserService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import javax.servlet.http.HttpSession;
import java.util.List;

@Service
public class UserServiceImpl implements UserService {

    @Resource
    private UserMapper mapper;

    @Override
    public boolean login(HttpSession session, String account, String password) {
        if (session.getAttribute("user") != null) return true;
        User user = mapper.getUserByAccount(account);
        if (user != null && user.getPassword().equals(password)) {
            session.setAttribute("user", user);
            return true;
        }
        return false;
    }

    @Override
    public List<User> getUsers() {
        return mapper.getUsers();
    }

    @Override
    public List<User> getUsersByName(String name) {
        return mapper.getUsersByName(name);
    }

    @Override
    public List<User> getUsersByPerm(String perm) {
        if (perm.equals("all")) return getUsers();
        return mapper.getUsersByPerm(perm);
    }

    @Override
    public User getUser(int uid) {
        return mapper.getUser(uid);
    }

    @Override
    public User getUserByAccount(String account) {
        return mapper.getUserByAccount(account);
    }

    @Override
    public boolean addUser(User user) {
        return mapper.addUser(user) == 1;
    }

    @Override
    public boolean deleteUser(int uid) {
        return mapper.deleteUser(uid) == 1;
    }

    @Override
    public boolean editUser(User user) {
        return mapper.editUser(user) == 1;
    }
}
